<?php
require_once "utils/db.php";
require_once "utils/util.php";
$id = explode("_", $_REQUEST['__id']);
$id = $id[0];
$qStringFieldsProperties =
"
			SELECT 
				Entity.`id` as EntityId,
				Entity.`name` as EntityName,
				Entity.`table_name` as EntityTable,
				Field.`id` as FieldId,
				Field.`name` as FieldName,
				Field.`type` as FieldType,
				Field.`db_field` as FieldDBField,
				Property.`property_name` as PropertyName,
				Property.`property_value` as PropertyValue
			FROM 
				`entities` AS Entity, 
				`entity_fields` AS Field
				LEFT JOIN `field_properties` Property ON Property.`field_id` = Field.`id`
			WHERE
				Entity.`id` = '".$id."' AND
				Field.`entity_id` = Entity.`id`
			ORDER BY Field.`form_location`
			";
//echo nl2br($qStringFieldsProperties);
$db = new db();
$fieldProperties = $db->rawSelect($qStringFieldsProperties);
$db->close();
$fields = array ();
//vp($fieldProperties);
/*
foreach ($fieldProperties as $val)
{
	if (! isset ($fields[$val['FieldId']])){
		//vp($val);
		$fields[$val['FieldId']] = "name:'".$val['FieldDBField']."', ".$val['PropertyName'].":'".$val['PropertyValue']."'";
	}
	else{
		$fields[$val['FieldId']] .= ", ".$val['PropertyName'].":'".$val['PropertyValue']."'";
	}
    
    if (! isset ($fields[$val['FieldId']]))
    {
    	//$fields[$val['FieldId']] = "name:'".$val['FieldDBField']."', ".$val['PropertyName'].":'".$val['PropertyValue']."'";
        if ( isset ($val['PropertyName']) && isset ($val['PropertyValue'])){
     		$fields[$val['FieldId']] = "name:'".$val['FieldDBField']."',fieldLabel:'".$val['FieldName']."',".$val['PropertyName'].":'".$val['PropertyValue']."'";
        }
		else{
			$fields[$val['FieldId']] = "name:'".$val['FieldDBField']."',fieldLabel:'".$val['FieldName']."'";
		}
    }
	
}vp($fieldProperties);
*/

foreach ($fieldProperties as $val)
{
    if (! isset ($fields[$val['FieldId']]))
    {
        if ( isset ($val['PropertyName']) && isset ($val['PropertyValue'])){
     		$fields[$val['FieldId']] = "fieldLabel:'".$val['FieldName']."', xtype :'".strtolower($val['FieldType'])."', name:'".$val['FieldDBField']."',".$val['PropertyName'].":'".$val['PropertyValue']."'";
        }
		else{
			$fields[$val['FieldId']] = "fieldLabel:'".$val['FieldName']."'";
		}
    }
}
$jsonFields = "";
foreach ($fields as $key=>$val)
{
    $jsonFields .= ",{".$val."}";
}
$jsonFields .= ",{name:'entityId',inputType:'hidden',value:'".$_REQUEST['__id']."'},{name:'MAX_FILE_SIZE',inputType:'hidden',value:'10000000'}";
$jsonFields = substr($jsonFields, 1);
$returnedJson = "{[".$jsonFields."]}";
print $returnedJson;
/*
 vp($fields);
 vp($fieldProperties);
 print('
 {
 [
 {
 fieldLabel		: "Autor",
 allowBlank		: false,
 blankText		: "Nu e completat",
 text			: "sdasd",
 width			: 300
 },
 new Ext.form.HtmlEditor({
 fieldLabel		: "Text stire",
 width			: 500,
 height			: 300
 })
 ]
 }
 ');
 */
?>
